<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\GuildUser;
use App\Service\GuildUserService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Pagination\LengthAwarePaginator;

class GuildController extends Controller
{
    public $v = 'admin.guild.';
	public $paginate = 15;

	/**
	 * 公会列表
	 * @param Request $request
	 * @return type
	 */
	public function index(Request $request){
		$search = $request->input('search');
		$keyword = $request->input('keyword');
		
		$builder = (new GuildUser())->orderBy('id', 'desc');
		//搜索
		if(!empty($search) && !empty($keyword)) {
			$builder = $builder->where($search, 'like', '%'.$keyword.'%');
		}
        $guild = $builder->paginate($this->paginate);
		$guildlist = $guild->map(function ($list) {
			$list->gtitle = (new \App\Service\ApiService())->getGuildTitle($list->gid)['title'];
			return $list;		
		});
        return view($this->v . 'index', compact('search', 'keyword', 'guild', 'guildlist'));
	}
	/**
	 * 公会添加
	 * @return type
	 */
	public function create()
    {
        return view($this->v . 'create');
    }
	/**
	 * 公会添加请求
	 * @return type
	 */
	public function store(Request $request, GuildUserService $guildUserService){
		$params = $request->all();
		$res = $guildUserService->createGuild($params);
		if($res['status'] == -1) {
			return ajaxError($res['error']);
		} elseif($res['status'] == 0) {
			return ajaxError();
		}
		return ajaxSuccess();		
	}
	/**
	 * 公会修改模板
	 * @param type $id
	 * @return type
	 */
	public function edit(Request $request, $id)
    {
        $guild = GuildUser::where('id', $id)->first();
		$guild->gtitle = (new \App\Service\ApiService())->getGuildTitle($guild->gid)['title'];
        return view($this->v . 'edit', compact('guild'));
    }
	/**
	 * 公会修改程序
	 * @param Request $request
	 * @param 
	 * @return type
	 */
    public function update(Request $request, GuildUser $guildUser, GuildUserService $guildUserService, $id)
    {
		$params = $request->all();		
		$res = $guildUserService->updateGuild($params, $guildUser, $id);
		if($res['status'] == -1) {
			return ajaxError($res['error']);
		} elseif($res['status'] == 0) {
			return ajaxError();
		}
		return ajaxSuccess();
    }
	/**
	 * 公会添加搜索用户
	 * @return type
	 */
	public function guildsearch(Request $request)
    {
		$username = $request->input('username');
		$rest = (new \App\Service\ApiService())->getGuildShow($username);
		return response()->json($rest);
    }
	/**
	 * 禁用-启用
	 * @param 
	 * @param 
	 * @return type
	 */
	public function updateStatus(Request $request, GuildUser $guildUser, $id)
    {
        $field = $request->input('field');
        $val = $request->input('val');
		$res = $guildUser->where('id', $id)->first();
        $res->{$field} = $val;
        if ($res->save()) return ajaxSuccess();
        return ajaxError();
    }
	/**
	 * 查看主播列表
	 * @param 
	 * @param 
	 * @return type
	 */
	public function anchor(Request $request, $gid) {
		$anchor = [];
		$search = $request->query('keyword');
		$status = $request->query('status') ?? 1;
		//$perPage = 3;
        //当前页数 默认1
        $page = $request->page ?: 1;
        //计算每页分页的初始位置
        $offset = ($page * $this->paginate) - $this->paginate;
		$rest = (new \App\Service\ApiService())->getAnchor(['gid' => $gid, 'search' => $search, 'status' => $status]);
		if($rest['total'] != 0) {
			$anchor= new LengthAwarePaginator(array_slice($rest['info'], $offset, $this->paginate, true), $rest['total'], $this->paginate,
				$page, ['path' => $request->url(), 'query' => $request->query()]);
		}       
		return view($this->v . 'anchor', compact('anchor', 'gid', 'search', 'status'));
	}
}